package br.com.salao.model;

import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import org.apache.log4j.Logger;

import br.com.salao.entity.Pais;
import br.com.salao.session.PaisBase;
import br.com.salao.util.Utils;

public class PaisModel {

	private Logger logger = Logger.getLogger(PaisModel.class.getName());

	public boolean inserir(Pais pais){
		try {	
			PaisBase paisBase = getBean();
			logger.info(pais.getNome());
			paisBase.inserir(pais);			
			return true;
		}catch (Exception e) {
			logger.error(e.getStackTrace() + e.getMessage() + e.getCause());
			e.printStackTrace();
			return false;
		}
	}

	public List<Pais> listarPaises(){		
		try {	
			PaisBase paisBase = getBean();	
			return paisBase.listarPaises();
		}catch (Exception e) {
			logger.error(e.getStackTrace() + e.getMessage() + e.getCause());
			return null;
		}
	}

	public Pais localizarPaisPorId(int idpais){
		try {	
			PaisBase paisBase = getBean();
			return paisBase.localizarPaisPorId(idpais);
		}catch (Exception e) {
			logger.error(e.getStackTrace() + e.getMessage() + e.getCause());
			e.printStackTrace();
			return null;
		}
	}
    public PaisBase getBean() throws Exception {
    	
        Properties prop = Utils.getProperties();     
        Context ctx = new InitialContext(prop); 
        Object ref = ctx.lookup(prop.getProperty("PAIS_LOOKUP"));       
      
        return (PaisBase)PortableRemoteObject.narrow(ref, PaisBase.class);
    }
}
